﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.IO;

namespace NSServer
{

    public sealed class Data
    {
        #region Variables

        //ebben tároljuk az embereket
        private static List<Ember> _emberList = new List<Ember>();

        private static readonly Object _lock = new Object();

        private static Data _instance = null;

        #endregion

        /// <summary>
        ///  az egyke Data legyártása (egyszer, (lock alatt)) és a példány visszaadása
        /// </summary>
        public static Data instance
        {
            get
            {
                lock (_lock)
                {
                    if (_instance == null)
                    {
                        _instance = new Data();
                    }
                }

                return _instance;
            }
        }

        /// <summary>
        ///  a személyek listája, akik regisztrálva vannak
        /// </summary>
        public static List<Ember> emberlist
        {
            get
            {
                return _emberList;
            }
        }

        /// <summary>
        ///  HDD-ről olvasás
        /// </summary>
        public static void adatfelolvasas()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Ember>));

            TextReader ReadFileStream = new StreamReader(Properties.Settings.Default.data_path + "Emberek.xml", false);

            _emberList = (List<Ember>)serializer.Deserialize(ReadFileStream);

            ReadFileStream.Close();
        }
        /// <summary>
        /// adatok HDD-re
        /// </summary>
        public static void adatKiiras()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Ember>));

            TextWriter WriteFileStream = new StreamWriter(Properties.Settings.Default.data_path + "Emberek.xml", false);

            serializer.Serialize(WriteFileStream, _emberList);

            WriteFileStream.Close();
        }
    }
}